package com.fengye.school.service;

import com.fengye.school.model.school.EClass;
import com.fengye.school.model.school.Lesson;
import com.fengye.school.model.school.Lesson;
import com.fengye.school.response.ResponsePageResult;
import com.fengye.school.respository.LessonRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class LessonService {

    @Resource
    private LessonRepository lessonRepository;

    public Lesson save(Lesson lesson){
        Lesson save = lessonRepository.save(lesson);
        log.info("保存成功, course={}", save);
        return save;
    }

    public List<Lesson> list() {
        return lessonRepository.findAll();
    }

    public ResponsePageResult<Lesson> listByPage(int page, int size) {
        Pageable pageRequest = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        Page<Lesson> all = lessonRepository.findAll(pageRequest);
        return ResponsePageResult.success("获取所有教学信息成功", all.getTotalElements(), all.getContent());
    }

    public void deleteAll(Long[] ids) {
        Arrays.stream(ids).forEach(id -> lessonRepository.deleteById(id));
    }

    public void deleteById(Long id) {
        lessonRepository.deleteById(id);
    }
}
